//
//  CZD_TiHuoDetailVC.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2020/9/27.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "CZD_TiHuoDetailVC.h"
#import "CZD_DetailHeadView.h"
#import "CaoZuoDanMainModel.h"
#import "CZD_DetailTiHuoCell.h"
#import "TiDanGoodListVC.h"

@interface CZD_TiHuoDetailVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) CZD_DetailHeadView *headView;
@property (nonatomic, strong) CaoZuoDanMainModel *model;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation CZD_TiHuoDetailVC

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.headView.nim_bottom, ScreenW, ScreenH - NaviHeight - self.headView.height - BottomHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakself requestForData];
        }];
        _tableView.hidden = YES;
    }
    return _tableView;
}

-(CaoZuoDanMainModel *)model {
    if (!_model) {
        _model = [CaoZuoDanMainModel new];
    }
    return _model;
}

-(CZD_DetailHeadView *)headView {
    if (!_headView) {
        _headView = [CZD_DetailHeadView new];
        _headView.frame = CGRectMake(0, 0, ScreenW, self.headView.height);
    }
    return _headView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"操作单详情";
    [self.view addSubview:self.headView];
    [self.view addSubview:self.tableView];
    [self requestForData];
}

- (void)requestForData {
    [HttpRequest PostWithApi:LogisticsSupplierCaoZuoDanQueryDetail params:@{@"o_id":self.o_id} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.model = [CaoZuoDanMainModel mj_objectWithKeyValues:responseObject];
            self.headView.model = self.model;
            [self.tableView reloadData];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.model.delivery_list.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CZD_DetailTiHuoCell *cell = [CZD_DetailTiHuoCell CZD_DetailTiHuoCellWithTableView:tableView];
    CaoZuoDanDiaoDuModel *model = self.model.delivery_list[indexPath.row];
    cell.model = model;
    __weak_self;
    cell.goodListBlock = ^{
        TiDanGoodListVC *vc = [TiDanGoodListVC new];
        vc.dp_id = model.dp_id;
        [weakself.rt_navigationController pushViewController:vc animated:YES];
    };
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CZD_DetailTiHuoCell *cell = [CZD_DetailTiHuoCell CZD_DetailTiHuoCellWithTableView:tableView];
    CaoZuoDanDiaoDuModel *model = self.model.delivery_list[indexPath.row];
    cell.model = model;
    return cell.height;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

@end
